package com.zsc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsc.constant.StatusConstant;
import com.zsc.dto.CategoryDTO;
import com.zsc.dto.CategoryPageQueryDTO;
import com.zsc.entity.Category;
import com.zsc.mapper.CategoryMapper;
import com.zsc.result.PageResult;
import com.zsc.service.CategoryService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author cjr165123050
 * @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
 * @createDate 2024-05-19 11:14:08
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Override
    public List<Category> list() {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", StatusConstant.ENABLE)
                .orderByDesc("update_time");
        return this.list(queryWrapper);
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        return this.list(new LambdaQueryWrapper<Category>().eq(Category::getType, type).orderByAsc(Category::getSort));
    }

    /**
     * 分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        Page<Category> categories = new Page<>(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();

        // 添加排序从小到大
        categoryLambdaQueryWrapper.orderByAsc(Category::getSort);

        // 检查并添加name条件
        if (categoryPageQueryDTO.getName() != null && !categoryPageQueryDTO.getName().isEmpty()) {
            categoryLambdaQueryWrapper.like(Category::getName, categoryPageQueryDTO.getName());
        }

        // 检查并添加type条件
        if (categoryPageQueryDTO.getType() != null) {
            categoryLambdaQueryWrapper.eq(Category::getType, categoryPageQueryDTO.getType());
        }

        Page<Category> page = this.page(categories, categoryLambdaQueryWrapper);
        return new PageResult(page.getTotal(), page.getRecords());
    }

    /**
     * 新增菜品分类
     *
     * @param categoryDTO
     * @return
     */
    @Override
    public boolean save(CategoryDTO categoryDTO) {
        Category category = new Category();
        category.setId(categoryDTO.getId());
        category.setType(categoryDTO.getType());
        category.setName(categoryDTO.getName());
        category.setSort(categoryDTO.getSort());
        return this.save(category);
    }

    /**
     * 启用禁用分类
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Category category = this.getById(id);
        category.setStatus(status);
        this.updateById(category);
    }


    /**
     * 修改分类
     *
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        category.setId(categoryDTO.getId());
        category.setName(categoryDTO.getName());
        category.setSort(categoryDTO.getSort());
        category.setType(categoryDTO.getType());
        this.updateById(category);
    }

    /**
     * 删除分类
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        this.removeById(id);
    }
}
